|
1
|
|
|
/** |
|
2
|
|
|
* Yii Sms Captcha widget. |
|
3
|
|
|
* |
|
4
|
|
|
* This is the JavaScript widget used by the yii\captcha\Captcha widget. |
|
5
|
|
|
* |
|
6
|
|
|
* @link http://www.yiiframework.com/ |
|
7
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
|
8
|
|
|
* @license http://www.yiiframework.com/license/ |
|
9
|
|
|
* @author Qiang Xue <[email protected]> |
|
10
|
|
|
* @since 2.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
(function ($) { |
|
13
|
|
|
$.fn.yiiSmsCaptcha = function (method) { |
|
14
|
|
|
if (methods[method]) { |
|
15
|
|
|
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); |
|
16
|
|
|
} else if (typeof method === 'object' || !method) { |
|
17
|
|
|
return methods.init.apply(this, arguments); |
|
18
|
|
|
} else { |
|
19
|
|
|
$.error('Method ' + method + ' does not exist on jQuery.yiiSmsCaptcha'); |
|
20
|
|
|
return false; |
|
21
|
|
|
} |
|
22
|
|
|
}; |
|
23
|
|
|
|
|
24
|
|
|
var defaults = { |
|
25
|
|
|
refreshUrl: undefined, |
|
26
|
|
|
hashKey: undefined, |
|
27
|
|
|
mobileField: undefined, |
|
28
|
|
|
buttonTime: undefined, |
|
29
|
|
|
buttonGet: undefined |
|
30
|
|
|
}; |
|
31
|
|
|
|
|
32
|
|
|
var countdown; |
|
33
|
|
|
|
|
34
|
|
|
var mobileReg = /^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/; |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
var addCookie = function (name, value, expiresHours) { |
|
37
|
|
|
var cookieString = name + "=" + value; |
|
38
|
|
|
if (expiresHours > 0) { |
|
39
|
|
|
var date = new Date(); |
|
40
|
|
|
date.setTime(date.getTime() + expiresHours * 1000); |
|
41
|
|
|
cookieString = cookieString + ";expires=" + date.toUTCString(); |
|
42
|
|
|
} |
|
43
|
|
|
document.cookie = cookieString; |
|
44
|
|
|
}; |
|
45
|
|
|
|
|
46
|
|
|
var editCookie = function (name, value, expiresHours) { |
|
47
|
|
|
var cookieString = name + "=" + value; |
|
48
|
|
|
if (expiresHours > 0) { |
|
49
|
|
|
var date = new Date(); |
|
50
|
|
|
date.setTime(date.getTime() + expiresHours * 1000); //单位是毫秒 |
|
51
|
|
|
cookieString = cookieString + ";expires=" + date.toGMTString(); |
|
52
|
|
|
} |
|
53
|
|
|
document.cookie = cookieString; |
|
54
|
|
|
}; |
|
55
|
|
|
|
|
56
|
|
|
var getCookie = function (name) { |
|
57
|
|
|
var strCookie = document.cookie; |
|
58
|
|
|
var arrCookie = strCookie.split("; "); |
|
59
|
|
|
for (var i = 0; i < arrCookie.length; i++) { |
|
60
|
|
|
var arr = arrCookie[i].split("="); |
|
61
|
|
|
if (arr[0] === name) { |
|
62
|
|
|
return arr[1]; |
|
63
|
|
|
break; |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
}; |
|
67
|
|
|
|
|
68
|
|
|
var methods = { |
|
69
|
|
|
init: function (options) { |
|
70
|
|
|
return this.each(function () { |
|
71
|
|
|
var $e = $(this); |
|
72
|
|
|
//用当前设置合并默认设置 |
|
73
|
|
|
var settings = $.extend({}, defaults, options || {}); |
|
74
|
|
|
$e.data('yiiSmsCaptcha', { |
|
75
|
|
|
settings: settings |
|
76
|
|
|
}); |
|
77
|
|
|
|
|
78
|
|
|
//绑定事件 |
|
79
|
|
|
$e.on('click.yiiSmsCaptcha', function () { |
|
80
|
|
|
methods.refresh.apply($e); |
|
81
|
|
|
return false; |
|
82
|
|
|
}); |
|
83
|
|
|
//检查倒计时完了没 |
|
84
|
|
|
var waitTime = getCookie(settings.hashKey) ? getCookie(settings.hashKey) : 0; |
|
85
|
|
|
if (waitTime > 0) { |
|
86
|
|
|
methods.remainTime(settings.hashKey, $e); |
|
87
|
|
|
} |
|
88
|
|
|
}); |
|
89
|
|
|
}, |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
refresh: function () { |
|
93
|
|
|
var $e = this, |
|
94
|
|
|
settings = this.data('yiiSmsCaptcha').settings; |
|
95
|
|
|
|
|
96
|
|
|
$.ajax({ |
|
97
|
|
|
url: $e.data('yiiSmsCaptcha').settings.refreshUrl, |
|
98
|
|
|
data: {"mobile": $("#" + settings.mobileField).val()}, |
|
99
|
|
|
dataType: 'json', |
|
100
|
|
|
type: 'POST', |
|
101
|
|
|
async: false, |
|
102
|
|
|
cache: false, |
|
103
|
|
|
success: function (data) { |
|
104
|
|
|
//刷新成功开始倒计时 |
|
105
|
|
|
addCookie(settings.hashKey, data.waitTime, data.waitTime); |
|
106
|
|
|
methods.remainTime(settings.hashKey, $e); |
|
107
|
|
|
$('body').data(settings.hashKey, data.hash); |
|
108
|
|
|
console.info(data); |
|
109
|
|
|
}, |
|
110
|
|
|
error: function (XMLHttpRequest, textStatus, errorThrown) { |
|
|
|
|
|
|
111
|
|
|
console.error("Http Error:" + XMLHttpRequest.statusText + ", status:" + XMLHttpRequest.status + ", responseText:" + XMLHttpRequest.responseText); |
|
112
|
|
|
} |
|
113
|
|
|
}); |
|
114
|
|
|
}, |
|
115
|
|
|
|
|
116
|
|
|
destroy: function () { |
|
117
|
|
|
return this.each(function () { |
|
118
|
|
|
$(window).unbind('.yiiSmsCaptcha'); |
|
119
|
|
|
$(this).removeData('yiiSmsCaptcha'); |
|
120
|
|
|
}); |
|
121
|
|
|
}, |
|
122
|
|
|
|
|
123
|
|
|
data: function () { |
|
124
|
|
|
return this.data('yiiSmsCaptcha'); |
|
125
|
|
|
}, |
|
126
|
|
|
|
|
127
|
|
|
remainTime: function (hashKey, e) { |
|
128
|
|
|
countdown = getCookie(hashKey) ? getCookie(hashKey) : 0; |
|
129
|
|
|
if (countdown === 0) { |
|
130
|
|
|
e.removeAttr("disabled"); |
|
131
|
|
|
e.html(e.data('yiiSmsCaptcha').settings.buttonGet); |
|
132
|
|
|
return; |
|
133
|
|
|
} else { |
|
134
|
|
|
e.attr("disabled", true); |
|
135
|
|
|
e.html(e.data('yiiSmsCaptcha').settings.buttonTime.replace(/@second@/, countdown)); |
|
136
|
|
|
countdown--; |
|
137
|
|
|
editCookie(hashKey, countdown, countdown + 1); |
|
138
|
|
|
} |
|
139
|
|
|
setTimeout(function () { |
|
140
|
|
|
methods.remainTime(hashKey, e); |
|
141
|
|
|
}, 1000); |
|
142
|
|
|
} |
|
143
|
|
|
}; |
|
144
|
|
|
})(window.jQuery); |
|
145
|
|
|
|
|
146
|
|
|
yii.validation.smsCaptcha = function (value, messages, options) { |
|
|
|
|
|
|
147
|
|
|
if (options.skipOnEmpty && pub.isEmpty(value)) { |
|
|
|
|
|
|
148
|
|
|
return; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
// CAPTCHA may be updated via AJAX and the updated hash is stored in body data |
|
152
|
|
|
var hash = jQuery('body').data(options.hashKey); |
|
153
|
|
|
if (hash == null) { |
|
154
|
|
|
hash = options.hash; |
|
155
|
|
|
} |
|
156
|
|
|
for (var h = 0, i = value.length - 1; i >= 0; --i) { |
|
157
|
|
|
h += parseInt(value.charAt(i), 10); |
|
158
|
|
|
} |
|
159
|
|
|
if (h !== hash) { |
|
160
|
|
|
yii.validation.addMessage(messages, options.message, value); |
|
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
}; |
|
163
|
|
|
|
|
164
|
|
|
|